Re: Sequences - Mailing list pgsql-sql
From | Chris Ruprecht |
---|---|
Subject | Re: Sequences |
Date | |
Msg-id | p05101205b88da4231f87@[192.168.0.6] Whole thread Raw |
In response to | Re: Sequences ("Hunter, Ray" <rhunter@enterasys.com>) |
List | pgsql-sql |
<div>Ray,</div><div><br /></div><div>You can certainly store the value in a variable, how to do that depends on the languageyou're using. If you're running PL/pgSQL, then you would have to define the variable somewhere on top and then use</div><div> </div><div>var_num := ...</div><div><br /></div><div>Best regards,</div><div>Chris</div><div><br /></div><div>At10:12 -0500 02/11/2002, Hunter, Ray wrote:</div><blockquote cite="cite" type="cite"><font size="-1">The onlyproblem with this solution is that I have already incremented the sequence and need to use the current value? Is therea way to assign the value to a variable and then use it in an insert statement?</font><br /></blockquote><blockquotecite="cite" type="cite"><font size="-1">This is just a hashed out example. I am hoping it canall be done in sql statements...</font><br /><font size="-1">Example:</font><br /> <font size="-1"> var num = selectlast_value from user_table_id_seq;</font><br /></blockquote><blockquote cite="cite" type="cite"> <font size="-1">insert into users ( "user_id", "user_fname", "user_lname", "user_email" )</font><br /><font size="-1"> values( 'num', 'Ray', 'Hunter', 'rhunter@enterasys.com' );</font><br /></blockquote><blockquote cite="cite" type="cite"><br/></blockquote><blockquote cite="cite" type="cite"><font size="-1">If I could do this is would be great...Isit possible?</font><br /></blockquote><blockquote cite="cite" type="cite"><br /></blockquote><blockquote cite="cite"type="cite"><font size="-1">Thanks,</font><br /></blockquote><blockquote cite="cite" type="cite"> <br /></blockquote><blockquotecite="cite" type="cite"><font size="-1">Ray Hunter</font><br /><font size="-1">Firmware Engineer</font><br/></blockquote><blockquote cite="cite" type="cite"><font size="-1">ENTERASYS NETWORKS</font><br /></blockquote><blockquotecite="cite" type="cite"><br /></blockquote><blockquote cite="cite" type="cite"><font size="-1">-----OriginalMessage-----</font><br /><font size="-1">From: Bruno Wolff III [</font><a href="mailto:bruno@wolff.to"><fontsize="-1">mailto:bruno@wolff.to</font></a><font size="-1">]</font><br /><font size="-1">Sent:Monday, February 11, 2002 7:37 AM</font><br /><font size="-1">To: Hunter, Ray</font><br /><font size="-1">Cc:pgsql-sql@postgresql.org</font><br /><font size="-1">Subject: Re: [SQL] Sequences</font><br /></blockquote><blockquotecite="cite" type="cite"><br /></blockquote><blockquote cite="cite" type="cite"><font size="-1">OnMon, Feb 11, 2002 at 08:43:23AM -0500,</font><br /><font size="-1"> "Hunter, Ray" <rhunter@enterasys.com>wrote:</font><br /><font size="-1">> I have various sequences in my database set up for ids. My question</font><br /><font size="-1">> is: How can I get the current value of the sequence without creating a</font><br/><font size="-1">> session and using the currval function?</font><br /><font size="-1">></font><br /><fontsize="-1">> I would like to take the current value of the sequence and use it for</font><br /><font size="-1">>a value in an insert statement.</font><br /><font size="-1">></font><br /><font size="-1">></font><br/><font size="-1">> Example:</font><br /><font size="-1">></font><br /><font size="-1">> insert into users ( "user_id", "user_fname", "user_lname",</font><br /><font size="-1">> "user_email")</font><br /><font size="-1">> values ( 'current sequence', 'Ray', 'Hunter', 'rhunter@enterasys.com'</font><br/><font size="-1">> );</font><br /></blockquote><blockquote cite="cite" type="cite"><fontsize="-1">I think you want to use nextval in this context. currval is used when you have already gottena new sequence number and want to use it in several inserts in the same transaction.</font><br /></blockquote><blockquotecite="cite" type="cite"><font size="-1">If you make user_id a serial type, then its default valuewill be nextval and you could use: insert into users ( "user_fname", "user_lname", "user_email" )</font><br /></blockquote><blockquotecite="cite" type="cite"><font size="-1"> values ('Ray', 'Hunter', 'rhunter@enterasys.com' );</font><br/><font size="-1">to add a row.</font></blockquote><div><br /></div><div><br /></div><pre>-- </pre><div align="center"><font color="#0000FF">Chris Ruprecht</font></div><div align="center"><font color="#0000FF">Networkgrunt and bit pusher extraordinaíre</font></div> _________________________________________________________Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com